﻿using System.Collections.ObjectModel;
using System.Xml.Linq;

namespace Grinder.Fb2
{
    public sealed class DocumentInfo
    {
        #region Private Properties
        private readonly Collection<Author> _authors = new Collection<Author>();
        #endregion

        #region Public Properties
        public Collection<Author> Authors { get { return _authors; } }
        public string ProgramUsed { get; set; }
        public string Date { get; set; }
        public string SourceOcr { get; set; }
        public string Id { get; set; }
        public string Version { get; set; }
        public string History { get; set; }
        #endregion

        public DocumentInfo() { }

        public DocumentInfo(XContainer element)
        {
            // Parsing author information
            var authorEls = element.Elements(Constants.Xmlns + "author");
            if (authorEls != null)
                foreach (var authorEl in authorEls)
                    _authors.Add(new Author(authorEl));

            // Parse used programs
            var el = element.Element(Constants.Xmlns + "program-used");
            ProgramUsed = (el == null) ? "" : el.Value;

            // Parse date
            el = element.Element(Constants.Xmlns + "date");
            Date = (el == null) ? "" : el.Value;

            // Parse Source OCR Author
            el = element.Element(Constants.Xmlns + "src-ocr");
            SourceOcr = (el == null) ? "" : el.Value;

            // Get document id
            el = element.Element(Constants.Xmlns + "id");
            Id = (el == null) ? "" : el.Value;

            // Get version
            el = element.Element(Constants.Xmlns + "version");
            Version = (el == null) ? "" : el.Value;

            // Get document history
            el = element.Element(Constants.Xmlns + "history");
            History = (el == null) ? "" : el.Value;
        }
    }
}
